package day02;

import util.JdbcUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestTransaction {

    public static void main(String[] args) {

        Connection con=null;
        PreparedStatement ps=null;
        try{
            con= JdbcUtil.getConnection();

            con.setAutoCommit(false);

            String sql=new StringBuffer()
                    .append("insert into t_user ")
                    .append("    (name) ")
                    .append("values ")
                    .append("    (?) ")
                    .toString();

            ps=con.prepareStatement(sql);


            ps.setString(1,"test7");
            ps.executeUpdate();

            int i=1/0;


            ps.setString(1,"test8");
            ps.executeUpdate();


            con.commit();

        }catch (Exception e){
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally{
            JdbcUtil.close(null,ps,con);
        }



    }
}
